package com.tj.controller;

import com.tj.pojo.Orders;
import com.tj.pojo.Users;
import com.tj.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

// ================================= 查询用户 =================================
@RequestMapping("/getOrdersList")
public Map<String, Object> getOrdersList() throws Exception {
    List<Orders> ordersList = ordersService.findAllList();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("status", "1");
    map.put("ordersList",ordersList);
    return map;
}
    /*
     * -按ID查
     */
    @RequestMapping("/getOrdersListById")
    public Map<String, Object> getOrdersListById(@RequestParam String orderId) throws Exception {
        List<Orders> ordersList = ordersService.findOrdersListById(orderId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("ordersList",ordersList);
        return map;
    }
    // ================================= 增加用户 =================================
    @RequestMapping("/insertOrders")
    public int insertOrders(@RequestBody Orders orders) {
        return ordersService.insertOrders(orders);
    }

    // ================================= 修改用户 =================================
    @RequestMapping("/updateOrders")
    public int updateOrders(@RequestBody Orders orders) {
        return ordersService.updateOrders(orders);
    }
    // ================================= 删除用户 =================================
    /*
     * -执行用户删除的操作
     */
    @RequestMapping("/deleteOrders")
    public Map<String, Object> deleteOrders(HttpSession session, Orders orders) throws Exception {
        int num = ordersService.deleteOrders(orders);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("status", "1");
        } else {
            map.put("status", "0");
        }
        return map;
    }

}


